package com.glamour.hlcity.dal.typeHandler;

import java.sql.SQLException;

import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;

/**
 * 布尔型转换
 * @author zhaoxing.liuzx
 *
 */
public class YNBooleanTypeHandlerCallback implements TypeHandlerCallback {

	 private static final String FALSE = "N";
	    private static final String TRUE = "Y";

	    @Override
	    public Object getResult(ResultGetter getter) throws SQLException {
	        return valueOf(getter.getString());
	    }

	    @Override
	    public void setParameter(ParameterSetter setter, Object parameter) throws SQLException {
	        if (parameter == null) {
	            setter.setString(FALSE);
	            return;
	        }
	        setter.setString((Boolean) parameter ? TRUE : FALSE);
	    }

	    @Override
	    public Object valueOf(String s) {
	        if (s == null) {
	            return Boolean.FALSE;
	        }
	        return Boolean.valueOf(TRUE.equalsIgnoreCase(s.trim()));
	    }

}
